<script type="text/javascript">
$(function(){
	var pg = App.macRpc.svcIndex.ui, pp = pg.params,
		data, svcs,
		svcGd = pg.seek('svcGd'),
		infoPl = pg.children('.infoPl');

	svcGd.mac('grid', {
		key: 'name',
		cols : [{
			field: 'id', title: '', width: 20,
			render: function(r, tr, me) {
				var a = $('<span class="icon icon-plus"></span>');
				a.click(function(){
					var yy = me.find('.ybar>div'), pl = tr.exPanel;
					if (a.hasClass('icon-plus')) {
						if (pl) {
							pl.show();
						} else {
							pl = $('<div class="expand"></div>');
							var mm = r.methodConfigMap;
							for (var k in mm) {
								var m = mm[k];
								k = k.substr(k.lastIndexOf('/') + 1);
								$('<span class="a"></span>').click(function(){
									App.show({
										ctx: 'macRpc',
										action: 'svcMain',
										params: { svcId: r.id, data: data }
									}, $.noop, infoPl);
								}).append(k).appendTo(pl);
							}
							tr.exPanel = pl.insertAfter(tr);
							$('<div class="clear"></div>').insertBefore(pl);
						}
						yy.height(yy.height() + pl.height());
						a.removeClass('icon-plus').addClass('icon-minus');
					} else {
						pl.hide();
						yy.height(yy.height() - pl.height());
						a.removeClass('icon-minus').addClass('icon-plus');
					}
				});
				return a;
			}
		}, {
			field: 'key', title : '服务名', width: 360,
			render: function(r){
				return $('<span class="a"></span>').click(function(){
					App.show({
						ctx: 'macRpc',
						action: 'svcMain',
						params: { svcId: r.id, data: data }
					}, $.noop, infoPl);
				}).append(r.id || r.name);
			}
		},{
			field: 'version', title : '版本', width: 80
		}],
		pagerLength: 10
	});
	
	function showDetail(r){
		App.show({
			ctx: 'macRpc',
			action: 'svcMain',
			params: { svcId: r.id, data: data }
		}, $.noop, infoPl);
	}
	
	function adjust(){
		var h = pg.parent().height();
		pg.height(h);
		svcGd.parent().height(h - 16);
		svcGd.adjust();
		infoPl.height(h);
		infoPl.width(pg.width() - svcGd.width());
	}
	
	$(window).resize(adjust);
	adjust();
	
	function init(dd) {
		data = dd;
		//遍历所有注册表的serviceMap，找出并合并所有的服务
		svcs = {};
		$.each(dd.reg, function(a, n){
			$.each(n.serviceMap, function(k, s){
				svcs[k] = s;
			});
		});
		svcs = $.map(svcs, function(n){
			return n;
		});
		svcGd.loadData(svcs);
	}
	
// 	var w = mac.wait('Loading');
	$.get(App.ctx + '/mac/rpc/info.do', {
		addr: App.getHashParam('addr'),
		r: new Date().getTime()
	}, function(rs) {
// 		w.close();
		var ro = mac.eval(rs);
		if (ro.success) {
			init(ro.data);
		} else {
			mac.alert(ro.data, ro.params);
		}
	});

	pg.seek('srchBtn').click(function(e) {
		initdata($(pg.seek('key')).val());
	});
	
	pg.seek('key').keydown(function(e) {
		if (e.keyCode == 13)
			initdata($(pg.seek('key')).val());
	});
});
</script>
<div class="left fl">
<div class="head">
	&nbsp;&nbsp; 服务名：<input type="text" name="key" />
	<button name="srchBtn">查找</button>
</div>
	<div class="body">
		<div class="grid" name="svcGd"></div>
	</div>
</div>
<div class="infoPl"></div>